﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Ict.DataAccess.Conditions
{
    /// <summary>
    /// SQL Like Clause条件
    /// </summary>
    [Serializable]
    public class LikeCond : Condition
    {
        /// <summary>
        /// 零个或多个字符的任意字符串的通配符
        /// </summary>
        public const string MultipleWildcard = "%";
        /// <summary>
        /// 任何单个字符
        /// </summary>
        public const string SingleWildcard = "_";

        /// <summary>
        /// 输入参数text放置模式
        /// </summary>
        public enum LikeMode
        {
            /// <summary>
            /// 已包含通配符
            /// </summary>
            Original,
            /// <summary>
            /// 以输入字符串开头
            /// </summary>
            StartWith,
            /// <summary>
            /// 以输入字符串结尾
            /// </summary>
            EndWith,
            /// <summary>
            /// 包含输入字符串
            /// </summary>
            Contain
        }

        private string _fieldName;
        /// <summary>
        /// 字段名
        /// </summary>
        public string FieldName
        {
            get
            {
                return _fieldName;
            }
        }

        private string _text;
        /// <summary>
        /// 匹配字符串模式，包含通配符
        /// </summary>
        public string Text
        {
            get
            {
                return _text;
            }
        }

        /// <summary>
        /// 直接使用含通配符的模式生成Like条件
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="text">含通配符的模式</param>
        public LikeCond(string fieldName, string text) : 
            this(fieldName, text, LikeMode.Contain)
        {
            
        }

        /// <summary>
        /// 生成Like条件
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="text">模式串</param>
        /// <param name="mode">生成模式串方式</param>
        public LikeCond(string fieldName, string text, LikeMode mode)
        {
            _fieldName = fieldName;
            switch (mode)
            {
                case LikeMode.Contain:
                    _text = MultipleWildcard + ReplaceWildcard(text) + MultipleWildcard;
                    break;
                case LikeMode.StartWith:
                    _text = MultipleWildcard + ReplaceWildcard(text);
                    break;
                case LikeMode.EndWith:
                    _text = ReplaceWildcard(text) + MultipleWildcard;
                    break;
                default:
                    _text = text;
                    break;
            }
        }

        private string EscapeWildcard(Match m)
        {
            return "[" + m.ToString() + "]";
        }

        private string ReplaceWildcard(string s)
        {
            Regex r = new Regex(MultipleWildcard + "|" + SingleWildcard);
            return r.Replace(s, new MatchEvaluator(EscapeWildcard));
        }
    }
}
